/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ruchin.tek.employees.web;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.transaction.UserTransaction;
import ru.ruchin.tek.employees.data.EmployeeStorage;

/**
 * Web application lifecycle listener.
 *
 * @author yruchin
 */
public class ServletContextListener implements javax.servlet.ServletContextListener {
    @PersistenceContext
    EntityManager entityManager;
    @Resource
    UserTransaction utx;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.setProperty("hibernate.show_sql", "true");
        ServletContext sc = sce.getServletContext();
        sc.setAttribute(EmployeeStorage.CONTEXT_KEY, new EmployeeStorage(entityManager, utx));
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // do nothing
    }
}
